# coding=utf-8
from django.template import RequestContext
from academia.views_helper import build_menu
from django.shortcuts import render_to_response
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.decorators import login_required, permission_required


def login_dashboard(request):
    if request.POST:
        email = request.POST.get('email')
        password = request.POST.get('password')
        user = authenticate(username=email, password=password)

        url = 'dashboard/index.html'

        if user is not None:
            if user.is_active:
                login(request, user)
                return homepage(request)
            else:
                message = _("User not authorized, contact the administrator")
                url = 'dashboard/login.html'
        else:
            message = _("Email or password invalid")
            url = 'dashboard/login.html'

        return render_to_response(url, locals(), context_instance=RequestContext(request))

    return render_to_response('dashboard/login.html', locals(), context_instance=RequestContext(request))


@login_required
def logout_dashboard(request):
    logout(request)
    return render_to_response('dashboard/login.html', locals(), context_instance=RequestContext(request))


@login_required
def homepage(request):
    userMainMenus = build_menu(request)
    return render_to_response('dashboard/index.html', locals(), context_instance=RequestContext(request))


@login_required
@permission_required('list_group')
def groups(request):
    pass

@login_required
@permission_required('add_group')
def group(request):
    pass

@login_required
@permission_required('delete_group')
def group_delete(request, group):
    pass

@login_required
@permission_required('change_group')
def group_edit(request, group):
    pass